package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

  static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
  static final int PICK_CONTACT_REQUEST = 0;
  static final String STATE_EDIT_TEXT = "message";
  String message;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = getIntent();
    if (intent != null) {
      Tools.log(this, intent.getAction());
    }
    System.out.println("create");
  }

  @Override
  protected void onStart() {
    super.onStart();
    System.out.println("start");
  }

  @Override
  protected void onRestart() {
    super.onRestart();
    System.out.println("restart");
  }

  /**
   * activity 与用户交互，并保持这一状态
   */
  @Override
  protected void onResume() {
    super.onResume();
    System.out.println("resume");
  }

  /**
   * the user is leaving the activity
   * means the activity is no longer in the foreground(even if it's still visibly)
   */
  @Override
  protected void onPause() {
    super.onPause();
    System.out.println("pause");
  }

  @Override
  protected void onStop() {
    super.onStop();
    System.out.println("stop");
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    System.out.println("destroy");
  }

  public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
  }

  /**
   * activity 进入stop状态之后保存实列状态的回调
   *
   * @param outState
   */
  @Override
  protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putString(STATE_EDIT_TEXT, "this is a save test");

    System.out.println("save instance state");
  }

  /**
   * 系统销毁的activity再次打开时候，由系统传入key-value集合对象
   * 可以用来恢复一些状态
   *
   * @param savedInstanceState
   */
  @Override
  protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    //    message = savedInstanceState.getString(STATE_EDIT_TEXT);
    //
    //    EditText editText = findViewById(R.id.editText);
    //    editText.setText(message);

    System.out.println("restore instance state: " + savedInstanceState.getString(STATE_EDIT_TEXT));
  }

  /**
   * startActivityForResult 回调函数
   *
   * @param requestCode 请求code
   * @param resultCode  结果code
   * @param data        返回的数据
   */
  @Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_CONTACT_REQUEST) {
      Tools.log(this, resultCode);
      if (resultCode == RESULT_OK) {
        Uri contactUri = data.getData();
        contactUri.toString();
      }
    }
  }

  public void openContacts(View v) {
    Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(intent, PICK_CONTACT_REQUEST);
  }

  public void openCamera(View v) {
    startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 1);
  }

  public void openAnimation(View v) {
    startActivity(new Intent(this, Animation.class));
  }
}
